const Jimp = require('jimp');
const Fs = require('fs');
const processInfo = process.argv.splice(2);
const png_path = processInfo[0];
const out_path = processInfo[1];
console.log('png_path = ', png_path);

readDirSync(png_path, (path, name) => {
    Jimp.read(path, (err, image) => {
        if (err) {
            console.log(png_path + "转换失败");
            return;
        }
        if (out_path) {
            image.writeAsync(out_path + '/' + name + ".jpg").then(()=>{
                console.log(name + '转换成功');
            });
        } else {
            image.writeAsync("./output/" + name + ".jpg").then(()=>{
                console.log(name + '转换成功');
            });
        }
    }).finally();
})

function readDirSync(path, cb) {
    let pa = Fs.readdirSync(path);
    pa.forEach((ele, index) => {
        let info = Fs.statSync(path + "/" + ele);
        if (info.isDirectory()) {
            console.log("dir: " + ele);
            readDirSync(path + "/" + ele, cb);
        } else {
            console.log("file: " + ele);
            if (ele.includes(".png")) {
                cb && cb(path + "/" + ele, ele.replace(".png", ''));
            }
        }
    })
}